<?php

namespace app\admin\controller\wwh;

use app\common\controller\Backend;
use fast\Tree;

class Column extends Base
{
    protected $model = null;
    protected $tree = null;
    protected $templateList = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\wwh\Column;
        $this->tree = Tree::instance();

        // 公共变量
        $langList = $this->model->getLangList();
        $currentLang = $this->request->get('lang', '1');
        $this->templateList = \app\admin\model\wwh\Template::order('id asc')->select();

        $this->view->assign("templateList", $this->templateList);
        $this->view->assign('langList', $langList);
        $this->view->assign('currentLang', $currentLang);
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("classifyList", $this->model->getClassifyList());
        $this->assignconfig("classifyList", $this->model->getClassifyList());
    }

    /**
     * 栏目列表
     */
    public function index()
    {
        $lang = $this->request->get('lang', '1');
        if ($this->request->isAjax()) {
            $lang = $this->request->request('lang', $lang);
            $list = $this->model->where('lang', $lang)->order('weigh desc,id asc')->select();
            $list = collection($list)->toArray();
            $this->tree->init($list, 'parent_id');
            $treeList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
            $total = count($treeList);
            $result = ["total" => $total, "rows" => $treeList];
            return json($result);
        }

        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        $lang = $this->request->get('lang', '1');
        // 获取当前语言的栏目
        $list = $this->model->where('lang', $lang)->order('weigh desc,id asc')->select();
        $list = collection($list)->toArray();
        $this->tree->init($list, 'parent_id');
        $columnList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
        $this->view->assign('columnList', $columnList);

        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $lang = $row['lang'];
        // 获取当前语言的栏目
        $list = $this->model->where('lang', $lang)->order('weigh desc,id asc')->select();
        $list = collection($list)->toArray();
        $this->tree->init($list, 'parent_id');
        $columnList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
        $hasArchives = model('\app\admin\model\wwh\Archives')->where('column_id',$row['id'])->whereOr('FIND_IN_SET(:id, `column_ids`)', ['id' => $row['id']])->count();
        $this->view->assign('hasArchives', $hasArchives);
        $this->view->assign('columnList', $columnList);
        $this->view->assign('currentLang', $lang);
        $this->view->assign('row', $row);

        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $pk = $this->model->getPk();
            $wwh = model('\app\admin\model\wwh\Column')->where('parent_id', 'in', $ids)->select();
            if ($wwh) {
                $this->error('存在下级栏目，无法删除!');
            }
            $res = model('\app\admin\model\wwh\Column')->alias('a')->join('wwh_archives w', 'a.id = w.column_id')->where('a.id', 'in', $ids)->select();
            if ($res) {
                $this->error('栏目存在内容，无法删除!');
            }
            $count = model('\app\admin\model\wwh\Column')->where($pk, 'in', $ids)->delete();
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
    }
}
